package com.lmmob.autotest.uitest;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;


public class ValueGenerator {
    public static String getString(String value) {
        if (StringUtils.isBlank(value)) return value;

        if ((value.contains("str:"))) {
            Integer numbers = Integer.parseInt(value.split(":")[1]);
            return generateEng(numbers);
        }

        if ((value.contains("eng:"))) {
            Integer numbers = Integer.parseInt(value.split(":")[1]);
            return generateEng(numbers);
        }

        if ((value.contains("random"))) {
            return generateRadome(value);
        }

        if ((value.contains("num:"))) {
            Integer numbers = Integer.parseInt(value.split(":")[1]);
            return generateString(numbers, '1');
        }

        if (value.equals("NULL")) {
            return null;
        }
        return value;
    }

    private static String generateRadome(String prefix) {
        Calendar calendar = Calendar.getInstance();
        Date today = calendar.getTime();
        SimpleDateFormat timesdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return prefix + timesdf.format(today);
    }

    private static String generateString(Integer numbers, char givenChar) {
        StringBuilder result = new StringBuilder(numbers);
        for (int i = 0; i < numbers; i++) {
            result.append(givenChar);
        }
        return result.toString();
    }
    
    private static String generateEng(Integer numbers) {
    	StringBuffer buffer=new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    	StringBuffer sb=new StringBuffer();
		Random r=new Random();
		int range=buffer.length();
		for(int i=0;i<numbers;i++)
		{
			sb.append(buffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
    }
    
    private static String generateEmail()
	{
		StringBuffer buffer=new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
		StringBuffer sb=new StringBuffer();
		Random r=new Random();
		int range=buffer.length();
		for(int i=0;i<15;i++)
		{
			sb.append(buffer.charAt(r.nextInt(range)));
		}
		sb.append("@lmmobtest.com");
		return sb.toString();
	}
	

}
